package com.aaa.controller.system;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.LayUiTree;

import com.aaa.entity.Result;

import com.aaa.entity.system.Menu;
import com.aaa.entity.system.PageEnty;
import com.aaa.entity.system.Role;
import com.aaa.entity.system.User;
import com.aaa.service.system.RoleService;

import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2022-08-31 20:32:53
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private RoleService roleService;



    /**
     * @create by:
     * @description: 分页查询所有
     * @create time: 2022/9/7 14:05
     *  * @param pageSortEntity
     * @param
    * @return com.aaa.entity.Result
    */
    @RequestMapping(value = "/findAll.do")
    public Result queryAllRole(PageEnty pageEnty, Role role){
        Page<Role> roles = roleService.queryAllRolePage(pageEnty, role);
        if (ObjectUtil.isNotEmpty(roles)){
            return success(roles);
        }else {
            return fail();
        }
    }

    /**
     * @create by:
     * @description: 查询所有角色
     * @create time: 2022/9/7 17:11
     *  * @param null
    * @return
    */
    @RequestMapping(value = "/queryAllRole.do")
    public Result queryAllRoles(PageEnty pageEnty, Role role){
        Page<Role> roles = roleService.queryAllRolePage(PageEnty.noPage(), role);
        return success(roles);
    }

    /**
     * @create by:
     * @description: 查询所有
     * @create time: 2022/9/7 14:25
     *  * @param
    * @return com.aaa.entity.Result
    */
    @GetMapping("queryAllRole.do")
    public Result queryAllRole() {
        List<Role> roles = roleService.queryAllRole();
        return success(roles);
    }

    /**
     * @create by:
     * @description: 添加
     * @create time: 2022/9/7 14:25
     *  * @param tblRole
     * @param menus
     * @param session
    * @return com.aaa.entity.Result
    */
    @RequestMapping("/insertRole.do")
    public Result saveRole(Role role,String menus, HttpSession session){
        User userInfo = (User) session.getAttribute("user");
        role.setCreateBy(userInfo.getUsername());
        role.setCreateTime(new Date());
        role.setStatus("0");
        //保存
        Role saveRole = roleService.saveRole(role);
        if (saveRole!=null){
            //保存成功，将角色对应的菜单id存储起来
      /*      String[] split = menus.split(",");
            roleService.insertRoleMenu(saveRole.getRoleId(),split);*/
            return success("添加成功");
        }
        return fail("添加失败");
    }

    /**
     * @create by:
     * @description: 修改
     * @create time: 2022/9/7 14:26
     *  * @param tblRole
     * @param menus
     * @param session
    * @return com.aaa.entity.Result
    */
    @RequestMapping("/updateRole.do")
    public Result updateRole(Role role,String menus,HttpSession session){
        User userInfo = (User) session.getAttribute("user");
        role.setUpdateBy(userInfo.getUsername());
        role.setUpdateTime(new Date());
        //修改
        Role tblRole1 = roleService.updateRole(role);
        if (tblRole1!=null){
            //保存成功，将角色对应的菜单id存储起来
            String[] split = menus.split(",");
            roleService.updateRoleMenu(tblRole1.getRoleId(),split);
            return success("修改成功");
        }else {
            return fail("修改失败");
        }
    }

    /**
     * @create by:
     * @description: 删除
     * @create time: 2022/9/7 14:28
     *  * @param roleId
    * @return com.aaa.entity.Result
    */
    @RequestMapping("/deleteById.do")
    public Result deleteRole(Integer roleId){

        int deleteRole = roleService.deleteRole(roleId);
        if (deleteRole>0){
            return success("删除成功");
        }else{
            return  fail("删除失败");
        }
    }

    /**
     * @create by:
     * @description: 查询所有的菜单
     * @create time: 2022/9/7 18:56
     *  * @param
    * @return java.util.List<com.aaa.entity.LayUiTree>
    */
    @GetMapping("findAllMenu.do")
    public List<LayUiTree> findAllMenu() {
        List<Menu> menus = roleService.queryAllMenu();

        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == 0) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                //组件展开
                /*tree.setSpread(true);*/
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }

    /**
     * @param role:
     * @return MyReturn
     * @author W
     * @description 判断角色名是否重名
     * @date 2022/7/14 16:29
     */
    @RequestMapping("findRoleName.do")
    public Result findRoleName(Role role){
        boolean check=roleService.findRoleByName(role);
        if (check) {
            return success("角色名不重复");
        }
        return fail("角色名重复");
    }

    /**
     *@Author
     *@Description  判断角色关键字是否重复
     *@Date 10:47 2022/9/6
     *@Param
     * @return
     **/
    @RequestMapping("findRoleKey.do")
    public Result queryRoleKey(Role role){
        boolean b = roleService.findRoleKey(role);
        if (b){
            return success("关键字可用");
        }
        return fail("关键字重复");
    }

    /**
     * @create by:
     * @description: 页面跳转
     * @create time: 2022/9/7 13:55
     *  * @param modelAndView
    * @return org.springframework.web.servlet.ModelAndView
    */
    @RequestMapping(value = "/toShowRole.do")
    public ModelAndView toPlanRole(ModelAndView modelAndView){
        modelAndView.setViewName("system/showRole");
        return modelAndView;
    }

    @GetMapping("findSelectMenu.do")
    public List<Integer> findSelectMenu(int roleId) {
        return roleService.findSelectMenu(roleId);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne.do")
    public Role selectOne(Integer id) {
        return roleService.queryById(id);
    }

    /**
     * @param parentId:
     * @param menus:
     * @return java.util.List<com.sd.entity.LayUiTree>
     * @create by: IvanZ
     * @description : 递归得到孩子节点
     * @create time: 2020/12/20 1:12
     */
    public List<LayUiTree> getChildren(Integer parentId, List<Menu> menus) {
        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                //组件展开
                /*                tree.setSpread(true);*/
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }

}

